package com.sduept.nwld.dataserver.controller.dataview;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import org.joda.time.DateTime;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;

import com.sduept.bigdata.fault.report.etl.model.BxProtectDeviceAction;
import com.sduept.core.dao.SuperDAO;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 保护装置动作原始数据
 * @author lxj
 * @Date 2020年5月20日
 */
@Named
@ViewScoped
public class ProtectDeviceActionController extends AbstractController {


	/**
	 *
	 */
	private static final long serialVersionUID = -7248115640079529045L;

	@Autowired
	private SuperDAO manager;

	private List<BxProtectDeviceAction> datas = new ArrayList<BxProtectDeviceAction>();
	private LazyDataModel<BxProtectDeviceAction> model;
	// 查询条件 开始时间
	private Date start;
	// 查询条件 结束时间
	private Date end;
	private int countRow = 0;

	@PostConstruct
	public void init() {
		end = new DateTime().toDate();
		start = new DateTime().minusDays(1).toDate();
		query();
	}

	public void query() {
		model = new LazyDataModel<BxProtectDeviceAction>() {

			private static final long serialVersionUID = 1265756074009804360L;

			@Override
			public List<BxProtectDeviceAction> load(int first, int pageSize, String sortField, SortOrder sortOrder,
													Map<String, Object> filters) {
				datas = manager.findByCondition("mEventStartTime between ?1 and ?2", new Object[] {start, end}, " order by mEventStartTime desc", new int[] { first, first + pageSize }, true, BxProtectDeviceAction.class);
				return datas;
			}

			@Override
			public List<BxProtectDeviceAction> load(int first, int pageSize, List<SortMeta> multiSortMeta,
													Map<String, Object> filters) {
				datas = manager.findByCondition("mEventStartTime between ?1 and ?2", new Object[] {start, end}, " order by mEventStartTime desc", new int[] { first, first + pageSize }, true, BxProtectDeviceAction.class);
				return datas;
			}

			@Override
			public BxProtectDeviceAction getRowData(String rowKey) {
				for (BxProtectDeviceAction t : datas) {
					if (t.getmRID().equals(rowKey)) {
						return t;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(BxProtectDeviceAction object) {
				for (BxProtectDeviceAction t : datas) {
					if (t.getmRID().equals(object.getmRID())) {
						return t;
					}
				}
				return null;
			}
		};
		end = DateUtil.getDateByString(DateUtil.getStringDate(end, DateUtil.pattern_ymd235959),DateUtil.pattern_ymdhms);
		long rowCount = manager.count("mEventStartTime between ?1 and ?2", new Object[] {start, end}, BxProtectDeviceAction.class);
		countRow = (int) rowCount;
		model.setRowCount(countRow);
	}


	public LazyDataModel<BxProtectDeviceAction> getModel() {
		return model;
	}

	public void setModel(LazyDataModel<BxProtectDeviceAction> model) {
		this.model = model;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public int getCountRow() {
		return countRow;
	}

	public void setCountRow(int countRow) {
		this.countRow = countRow;
	}

	public List<BxProtectDeviceAction> getDatas() {
		return datas;
	}

	public void setDatas(List<BxProtectDeviceAction> datas) {
		this.datas = datas;
	}

}
